package com.example.addressbook;

import android.widget.ImageView;
import android.widget.TextView;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; 
import android.content.Intent; 
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.view.MenuItem;
import com.bumptech.glide.Glide;
import android.support.v7.app.ActionBar;
import android.support.design.widget.CollapsingToolbarLayout;
import android.view.MotionEvent;
import android.content.Context;
import android.graphics.Color;
import org.litepal.crud.DataSupport;

import java.util.*;
import java.io.*;

public class ItemDetailActivity extends AppCompatActivity {
	
	private boolean oldState;
	private boolean isCheck;
	private String name = null;
	
	@Override
	public void onCreate(Bundle savedIntanceState){
		super.onCreate(savedIntanceState);
		setContentView(R.layout.item_detail_activity);
		android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar)findViewById(R.id.toolbar_item_detail);
		final FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.item_fab);
		Intent intent = getIntent();
		String tag = intent.getStringExtra("tag");
		fab.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v){
				if( isCheck ){
					fab.setImageResource(R.drawable.ic_favorite_outline_grey600_48dp);
					fab.setColorFilter(Color.RED);
				}else{
					Glide.with(ItemDetailActivity.this).load(R.drawable.ic_favorite_black_48dp).into(fab);
					fab.setColorFilter(Color.RED);
				} 
				isCheck = !isCheck;
			}
		});
		
		String tel = null;
		String info = null;
		int picturePath = R.drawable.p1;
		if( tag.equals("people") ){
			
			//Glide.with(this).load(R.drawable.ic_favorite_outline_grey600_48dp).into(fab);

			People p = (People)intent.getSerializableExtra("people_data");
			name = p.getName();tel = p.getTelephone();info = p.getInformation();picturePath = p.getPicturePath();
			List<Friend> list = DataSupport.where("name = ?",name).find(Friend.class);
			if( list.size()==0 ){
				isCheck = oldState = false;
				fab.setImageResource(R.drawable.ic_favorite_outline_grey600_48dp);
				fab.setColorFilter(Color.RED);
			}else{
				isCheck = oldState = true;
				Glide.with(this).load(R.drawable.ic_favorite_black_48dp).into(fab);
				fab.setColorFilter(Color.RED);
			}
		}else{
			isCheck = oldState = true;
			Glide.with(this).load(R.drawable.ic_favorite_black_48dp).into(fab);
			fab.setColorFilter(Color.RED);
			Friend f = (Friend)intent.getSerializableExtra("friend_data");
			name = f.getName();tel = f.getTelephone();info = f.getInformation();picturePath = f.getPicturePath();
		}
		setSupportActionBar(toolbar);
		ActionBar actionBar = getSupportActionBar();
		if( actionBar != null  ){
			actionBar.setDisplayHomeAsUpEnabled(true);
		}
		CollapsingToolbarLayout coll = (CollapsingToolbarLayout)findViewById(R.id.collapsing_toolbar);
		coll.setTitle(name);

		
		ImageView imageView = (ImageView)findViewById(R.id.title_img);
		Glide.with(this).load(picturePath).into(imageView);
		TextView t1 = (TextView)findViewById(R.id.text_view_item_name);
		TextView t2 = (TextView)findViewById(R.id.text_view_item_tel);
		TextView t3 = (TextView)findViewById(R.id.text_view_item_info);
		t1.setText(name);
		t2.setText(tel);
		t3.setText(info);
	}
	
	@Override
	public void onDestroy(){
		Intent intent = new Intent();
		intent.putExtra("is_check",isCheck);
		intent.putExtra("old_state",oldState);
		intent.putExtra("people_name",name);
		setResult(RESULT_OK,intent);
		super.onDestroy();
	}
	
	@Override
	public void onBackPressed(){
		Intent intent = new Intent();
		intent.putExtra("is_check",isCheck);
		intent.putExtra("old_state",oldState);
		intent.putExtra("people_name",name);
		setResult(RESULT_OK,intent);
		finish();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch( item.getItemId() ){
			case android.R.id.home: finish(); return true;
		}
		return super.onOptionsItemSelected(item);
	}
}